using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;



using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace BadHabit.Factory {


    /// <summary>
    /// SOAP extension to add the factory headers at the web service messages.
    /// It should be configured at the app.config file: system.web / webServices / soapExtensionTypes.
    /// </summary>
    public class FactoryClientSOAPExtension : SoapExtension {

        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) {
            return null;
        }

        public override object GetInitializer(Type WebServiceType) {
            return null;
        }

        public override void Initialize(object initializer) {
            
        }
        
       

        public override void ProcessMessage(SoapMessage message) {

            // if is not connected, you can not call any web method
            if (!"RetriveTeamMemberRoles".Equals(message.MethodInfo.Name) && !"CurrentTeamMember".Equals(message.MethodInfo.Name) && !Client.IsConnected)                
                throw new Exceptions.NotConnectedException();


            

            FactoryService.FactorySOAPHeader fheader;            

            switch (message.Stage) {                    

                case SoapMessageStage.BeforeDeserialize:


                    Client.IsReadingData = false;
                    

                    break;
                
                // after deserialize an in message, check a security exception flag
                case SoapMessageStage.AfterDeserialize:

                    Client.IsReadingData = false;

                                        

                    if (message.Headers.Count > 0 && message.Headers[0] is FactoryService.FactorySOAPHeader) {
                        fheader = (FactoryService.FactorySOAPHeader)message.Headers[0];
                        if (fheader.SecurityException)
                            throw new System.Security.SecurityException();
                    }
                    break;

                // before serialize an out message, puts the client version on it
                case SoapMessageStage.BeforeSerialize:

                    Client.IsReadingData = true;


        
                    // sends the client version to the server
                    fheader = new FactoryService.FactorySOAPHeader();
                    fheader.FactoryClientMinVersion = FactoryClientVersion;
                    if (!Properties.Settings.Default.UseWindowsAuthentication) {

                        if (String.IsNullOrEmpty(Properties.Settings.Default.UserMemberId))
                            throw new System.Security.SecurityException();

                        if (String.IsNullOrEmpty(Properties.Settings.Default.UserAccessKey))
                            throw new System.Security.SecurityException();

                        fheader.UserMemberId = Properties.Settings.Default.UserMemberId;
                        fheader.UserAccessKey = Properties.Settings.Default.UserAccessKey;
                    }
                    fheader.UserCulture = System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag;
                    message.Headers.Add(fheader);
                    break;                               
            }
        }
         
         

        private string version = null;
        private string FactoryClientVersion {
            get {
                if (version != null)
                    return version;
                version = "unknow/debug";
                if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) {
                    Version v = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
                    version = v.ToString();
                }
                return version;
            }
        }

    }
}
